home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / xWindows definition files / Main w⁄ suspend.p < prev    next >
Encoding:
Text File  |  1993-12-11  |  7.2 KB  |  199 lines  |  [TEXT/PJMM]

  1. { This is a modified version of the Main program for MacStarter_pascal that processes }
  2. { suspend and resume events.  I found this necessary to get my program to convert the }
  3. { clipboard on suspend or resume so that I could cut and paste text between applications. }
  4. { I don't know why I needed this; it should have happened automatically. }
  5.  
  6. { If you use this main program, you have to include a size resource for the program, that }
  7. { sets the "Accept suspend events" flag.   (Also note:  When you make a new size resource, }
  8. { you have to change its ID number to -1, or it will have no effect. ) }
  9.  
  10. { All this only applies to compilied applications.  }
  11.  
  12. program Main;
  13.  
  14. uses
  15.     xWindow, applicationProcs;
  16.  
  17. var
  18.     appleMenu: MenuHandle;
  19.  
  20. procedure AboutBox;
  21.     var
  22.         junk: integer;
  23.     begin
  24.         ParamText(ApplicationLongName, AuthorName, StringOf(AuthorAddress1, chr(13), AuthorAddress2, chr(13), AuthorAddress3), CommentLine);
  25.         junk := Alert(128, nil);
  26.     end;
  27.  
  28. procedure DoAppleMenu (itemNum: integer);
  29. { Handles a command from the apple menu, either by displaying the about box for the }
  30. { application or loading a desk acessory; it is not necessary to change this procedure }
  31.     var
  32.         DAName: str255;    { name of desk accessory to be loaded (from the apple menu) }
  33.         junk: integer;
  34.     begin
  35.         if itemNum = 1 then
  36.             AboutBox
  37.         else begin
  38.                 GetItem(appleMenu, itemNum, DAName);
  39.                 junk := OpenDeskAcc(DAName);
  40.             end;
  41.     end;
  42.  
  43.  
  44. procedure HandleMenuEvent (selection: longint;
  45.                                 var done: boolean);
  46.     var
  47.         MenuNum: integer;      { The longint "selection" is decoded into the id of the menu, which }
  48.         ItemNum: integer;     {    is just its menuID, and the item number, which is just the }
  49.                                        {    position of the item counting from the top of the menu. }
  50.     begin
  51.         MenuNum := HiWord(Selection);
  52.         if MenuNum = 0 then                  { no menu item was actually chosen--no action }
  53.             Exit(HandleMenuEvent);
  54.         ItemNum := LoWord(Selection);
  55.         if MenuNum = 1 then
  56.             DoAppleMenu(itemNum)
  57.         else if MenuNum = 2 then
  58.             DoFileMenu(ItemNum, done)
  59.         else if MenuNum = 3 then
  60.             DoEditMenu(ItemNum)
  61.         else
  62.             doOtherMenu(menuNum, itemNum);
  63.         HiliteMenu(0);    { A menu title is highlighted until HiliteMenu(0) is called. }
  64.     end;
  65.  
  66.  
  67.  
  68. procedure InstallMenus;
  69.     var
  70.         MBar: Handle;
  71.     begin
  72.         MBar := GetNewMBar(128);                         { Get the menu resource. }
  73.         SetMenuBar(MBar);                                      { Specify that this is the menu to be used }
  74.         appleMenu := GetMHandle(1);                        { Get a handle to the the apple menu. }
  75.         SetItem(appleMenu, 1, StringOf('About ', ApplicationShortName, '...'));
  76.         AddResMenu(appleMenu, 'DRVR');                  { Install all the desk accessaries onto the }
  77.                                                                             {     apple menu. }
  78.         editMenu := GetMHandle(3);                          { These provide GLOBAL variables for }
  79.         fileMenu := GetMHandle(2);                           {    referencing the edit and file menus; }
  80.         DrawMenuBar;                                              { This actually displays the Menu Bar and }
  81.                                                                            {     makes it active. }
  82.     end;
  83.  
  84.  
  85. procedure SendEventToWindow (win: windowPtr;
  86.                                 var theEvent: eventRecord);
  87.     var
  88.         X: XWindow;
  89.     begin
  90.         if Window2XWindow(win, x) then
  91.             X.doEvent(theEvent);
  92.     end;
  93.  
  94.  
  95.     var    { LOCAL VARIABLES FOR MAIN PROGRAM }
  96.  
  97.         done: boolean;
  98.         theEvent: EventRecord;   {  contains the event read by main event loop }
  99.         partNum: integer;
  100.         selection: longint;           { number encoding a menu selection }
  101.         whichWin: WindowPtr;    { the window that a mouseDown event occured in, or the }
  102.                                                { window of concern in an activate or update event. }
  103.         xWin: xWindow;
  104.         savePort: GrafPtr;
  105.         pt: point;
  106.         err: integer;
  107.         suspended: boolean;
  108.  
  109. begin
  110.  
  111.     InitXWindows;
  112.     InstallMenus;   { build the menu bar for this application }
  113.     InitializeApplication;
  114.  
  115.     FlushEvents(keyDownMask + autoKeyMask + mDownMask, 0);  { delete any events pending when}
  116.                                                                                                     { program starts. }
  117.  
  118.     InitCursor;  { changes cursor from watch to arrow; not needed inside THINK Pascal environment }
  119.  
  120.     done := false;  { this global variable is set to TRUE when an event occurs that causes the }
  121.                           { program to end.  (Program can also end via a Halt statement, as will }
  122.                           { happen if the procedure FatalError is called. }
  123.     err := TEFromScrap;
  124.     suspended := false;
  125.  
  126.     repeat   { This is the main event loop, which gets events and processes }
  127.                   { them until the program ends. }
  128.         if not WaitNextEvent(everyEvent, theEvent, maxSleepTime, nil) then begin{no event pending }
  129.                 if not suspended then
  130.                     ApplicationIdle
  131.             end
  132.         else begin  { process the event }
  133.                 case theEvent.what of
  134.                     keyDown:  begin
  135.                             if BitAnd(theEvent.modifiers, cmdKey) <> 0 then begin
  136.                                 { a key was pressed with the command key held down -- it needs to }
  137.                                 { be processed as a menu command. }
  138.                                     UpdateMenus;    { Enables/disables approporiate menu entries }
  139.                                     SetCursor(arrow);
  140.                                     Selection := MenuKey(chr(theEvent.message));  { which menu entry? }
  141.                                     HandleMenuEvent(Selection, done)
  142.                                 end
  143.                             else
  144.                                 SendEventToWindow(FrontWindow, theEvent);
  145.                         end;
  146.                     autoKey:   { note: don't allow autokey repeated command keys. }
  147.                         if (BitAnd(theEvent.modifiers, cmdKey) = 0) & (FrontWindow <> nil) then
  148.                             SendEventToWindow(FrontWindow, theEvent);
  149.                     mouseDown:  begin
  150.                             partNum := FindWindow(theEvent.where, whichWin);
  151.                             if partNum = inSysWindow then begin { this event is for a desk accessary or something }
  152.                                     SetCursor(arrow);
  153.                                     SystemClick(theEvent, whichWin)
  154.                                 end
  155.                             else if partNum = inMenuBar then begin
  156.                                     UpdateMenus;  { enable/disable appropriate menu entries }
  157.                                     SetCursor(arrow);
  158.                                     Selection := MenuSelect(theEvent.where);  { which menu entry? }
  159.                                     HandleMenuEvent(Selection, done);
  160.                                 end
  161.                             else if partnum <> inDesk then  { mouse event within a window }
  162.                                 SendEventToWindow(whichWin, theEvent)
  163.                         end;
  164.                     updateEvt, activateEvt:  begin
  165.                             whichWin := WindowPtr(theEvent.message);
  166.                             SendEventToWindow(whichWin, theEvent);
  167.                         end;
  168.                     app4Evt: 
  169.                         if (BitAnd(theEvent.message, $FF000000) = $01000000) then
  170.                             if BitAnd(theEvent.message, 1) = 1 then begin { resume }
  171.                                     suspended := false;
  172.                                     err := TEFromScrap;
  173.                                 end
  174.                             else begin
  175.                                     suspended := true;
  176.                                     err := ZeroScrap;
  177.                                     err := TEToScrap;
  178.                                 end;
  179.                     otherwise
  180.                    { do nothing for keyUp, mouseUp, multifinder events }
  181.                 end;
  182.             end;
  183.         if not suspended then begin
  184.                 whichWin := FrontWindow;
  185.                 if (whichWin <> nil) & (window2xWindow(whichWin, xWin)) then begin
  186.                         GetPort(savePort);
  187.                         SetPort(whichWin);
  188.                         GetMouse(pt);
  189.                         SetPort(savePort);
  190.                         xWin.adjustCursor(pt);
  191.                     end;
  192.             end;
  193.     until done;
  194.  
  195.     CleanUpApplication;
  196.     selection := ZeroScrap;
  197.     selection := TEToScrap;
  198.  
  199. end.